package com.moly.grpc.nio;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

public class QuickFileClient {

    public static void main(String[] args) {
        try {
            init();
            connect();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static SocketChannel socketChannel = null;
    private static Selector selector = null;

    private static boolean flag = true;
    public static void init() throws IOException {
        socketChannel = SocketChannel.open();
        selector = Selector.open();


        register(selector, socketChannel, false, SelectionKey.OP_CONNECT);

    }

    public static void connect() throws IOException {
        boolean connect = socketChannel.connect(new InetSocketAddress("localhost", 8899));
        while (flag){
            selector.select();

            Set<SelectionKey> keys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = keys.iterator();
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                handleKey(key);
                iterator.remove();
            }

        }
        selector.close();
        System.out.println("over");



    }

    public static void handleKey(SelectionKey key) throws IOException {

        if (key.isConnectable()) {
            SocketChannel socketChannel = (SocketChannel) key.channel();
            socketChannel.finishConnect();
            register(selector, socketChannel, false, SelectionKey.OP_READ);
        }
        if (key.isReadable()) {

            System.out.println("isReadable key.isValid() "+key.isValid());
            if(key.isValid()){
                SocketChannel socketChannel = (SocketChannel) key.channel();
                ByteBuffer buffer = ByteBuffer.allocate(16);
                socketChannel.read(buffer);
                buffer.flip();
                System.out.println("服务器发送  ：" + new String(buffer.array(), 0, buffer.limit()));

                register(selector, socketChannel, false, SelectionKey.OP_WRITE);
            }else {
                key.cancel();

            }


        }
        if(key.isWritable()){
            System.out.println("isWritable key.isValid() "+key.isValid());
            SocketChannel socketChannel = (SocketChannel) key.channel();

            System.out.println("请输入");
            InputStreamReader inputStreamReader = new InputStreamReader(System.in);
            CharBuffer buffer = CharBuffer.allocate(12);
            inputStreamReader.read(buffer);
            buffer.flip();
            String s = new String(buffer.array(), 0, buffer.length());
            socketChannel.write(ByteBuffer.wrap(s.getBytes()));
            if(s.contains("by")){
                key.cancel();
                socketChannel.close();
                flag = false;
            }





        }

    }

    private static void register(Selector selector, SelectableChannel channel, boolean block, int ops) throws IOException {
        channel.configureBlocking(block);
        channel.register(selector, ops);

    }
}
